﻿using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System;
using System.Collections;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using System.Collections.Generic;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SalesModule
{
	#region SalesPrice

    public class SalesPriceAppService : BaseAppService<SalesPrice, string, SalesPriceSearch, ISalesPriceRepository>, ISalesPriceAppService
	{
        private ICustomerAppService CustomerAppService { get; set; }
        private IStockAppService StockAppService { get; set; }

        public override BoolGenericResult<SalesPrice> AddOrUpdate(SalesPrice entity)
        {
            Init();
            this.CustomerAppService = this.SpringContext.GetObject(SystemModuleConst.CUSTOMERAPPSERVICE) as ICustomerAppService;
            this.StockAppService = this.SpringContext.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;

            BoolGenericResult<SalesPrice> result = new BoolGenericResult<SalesPrice>();
            IList<Customer> customers = new List<Customer>();
            foreach (Customer customer in entity.Customers)
            {
                customers.Add(this.CustomerAppService.GetById(customer.Id));
            }
            foreach (SalesPriceDetail salesPriceDetail in entity.SalesPriceDetails)
            {
                salesPriceDetail.Stock = this.StockAppService.GetById(salesPriceDetail.Stock.Id);
            }

            Employee employee = this.EmployeeAppService.GetSingleBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id));
            StockManager stockManager = new StockManager(employee);

            IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
            entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.SalesPrice);

            result = stockManager.AddSalesPrice(entity.Id, employee.Corporation.Id, entity.Type, entity.CustomerType, entity.StartDate, entity.EndDate,
                entity.Remark, entity.SalesPriceDetails, customers);

            if (result.IsSuccess)
            {
                base.AddOrUpdate(result.Object);
            }

            result.Messages.Add("保存成功");

            return result;
        }

        public override IEnumerable<SalesPrice> GetBySearch(SalesPriceSearch searchParams)
        {
            if (searchParams.CustomerCode.IsNotEmpty())
            {
                long total = 0;
                return this.Repository.GetPageList(searchParams, out total);
            }
            return base.GetBySearch(searchParams);
        }
    }
	#endregion
}